package com.nerve.android.cloudoffice.service.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nerve.util.DateUtil;

import com.alibaba.fastjson.JSON;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Customer;
import com.nerve.android.cloudoffice.service.NetService;

public final class CustomerData {
	
	private static final String URL = Const.HOST + "customer/";
	
	public static final Reply list(int userId, int type){
		Reply re = NetService.get(URL+"list.htm?id=" + userId + "&type=" + type);
		
		if(!re.isError()){
			try{
				List<Customer> customerL = JSON.parseArray(re.getMessage().toString(), Customer.class);
				
				re.setMessage(customerL);
			}catch(Exception e){
			}
		}
		
		return re;
	}
	
	private static final Map<String, String> buildMap(Customer c){
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("customer.name", c.getName());
		map.put("customer.address", c.getAddress());
		map.put("customer.qq", c.getQq());
		map.put("customer.phone", c.getPhone());
		map.put("customer.projectType", c.getProjectType()+"");
		map.put("customer.workType", c.getWorkType()+"");
		map.put("customer.foundation", c.getFoundation());
		map.put("startDate", DateUtil.toString(c.getStartDate(), "yyyy-MM-dd"));
		map.put("customer.price", c.getPrice()+"");
		map.put("customer.cycle", c.getCycle()+"");
		map.put("customer.remark", c.getRemark());
		map.put("customer.category", c.getCategory()+"");
		map.put("customer.time1", c.getTime1());
		map.put("customer.time2", c.getTime2());
		map.put("customer.time3", c.getTime3());
		map.put("customer.time4", c.getTime4());
		map.put("customer.id", c.getId()+"");
		map.put("id", c.getUser());
		
		return map;
	}
	
	public static final Reply save(Customer c){
		String url = (c.getId() == null || c.getId() == 0)?"save.htm" : "update.htm";
		Const.log("ADD Customer::::::::::::" + URL + url);
		Reply re = NetService.post(URL + url, buildMap(c));
		
		if(re.isError()){
			
		}
		
		return re;
	}
}
